#!/usr/bin/ruby
require 'rubygems'
require 'activeresource'
require 'mechanize'
require 'date'
require 'base64'
require 'net/smtp'
require 'iconv'


def parse_friends(page)
	doc = page.match(/'(\{"friends":[^']*\})'/)[1]
	friends = ActiveSupport::JSON.decode(doc)
	friends["friends"]
end

def parse_mycar(page)
	cars = ActiveSupport::JSON.decode(page)
	cars["carlist"]
end

def parse_park_place(page)
	scene = ActiveSupport::JSON.decode(page)
	j = 0
	places = []
	scene["parklist"].each do |park|
		if park["free"] == 0 and park["parked"] == 0 then 
			place = {}
			place["id"] = j
			place["user_name"] = scene["name"]
			place["user_id"] = scene["id"] 
			places << place
		end
		j = j + 1
	end
	places
end

def get_money(page)
	$mymoney = page.match(/flashvars.money = '([0-9]*)'/)[1]
	puts "您现在的现金为: #{$mymoney}元"
end

def get_myid(page)
	$myid = page.match(/pageData='\+'\{\"id\":([0-9]*)\}/)[1]
	puts "myid: #{$myid}"
end

def decode(string)
	if string != nil then
		string = string.gsub(/\\/, "")
		string.gsub(/u([0-9a-tv-z]{4})/) do |num|
			Iconv.conv("utf8", "ucs2", num.slice(3,2).to_i(16).chr + num.slice(1,2).to_i(16).chr) 
		end
	end
end

def remove_car()
	0.upto(3) do |i|
		$agent.post("http://carpark.xiaonei.com/ajax_car_park_remove_car.do", { :pid => i.to_s })
	end
end

def park_car(car, friends)
	friends.each do |friend|
		if friend["userid"].to_i != 940563 then
			url = "http://carpark.xiaonei.com/ajax_car_park_get_next_page.f?pid=#{friend["userid"]}"
			page = $agent.get_file(url)
			park_place = parse_park_place(page)
			if park_place.size != 0 and car["user_id"] != park_place[0]["user_id"] then
				isfree = car["park_type"] == nil ?	0 : 1
				args = { :carId => car["car_id"].to_s, :carName => decode(car["car_name"]), :free => isfree.to_s,
					:onlyCarId => car["uc_id"].to_s, :ownerId => $myid.to_s,
					:parkIdFrom => 4.to_s, :parkIdTo => park_place[0]["id"].to_s, 
					:parkOwnerIdFrom => car["user_id"] == nil ? 0.to_s : car["user_id"].to_s, 
					:parkOwnerIdTo => park_place[0]["user_id"].to_s, 
					:parkOwnerNameFrom => decode(car["user_name"]), :parkOwnerNameTo => decode(park_place[0]["user_name"]) }
				$agent.post("http://carpark.xiaonei.com/ajaxAddCar.do", args)
				puts "小的已经把你的[#{decode(car["car_name"])}]从[#{decode(car["user_name"])}]的私家车位停放到[#{decode(park_place[0]["user_name"])}]的私家车位"
				break
			end
		end
	end
end

begin
	$agent = WWW::Mechanize.new
	$agent.user_agent_alias = "Windows IE 6"
	puts "[程序]: <#{Time.now()}> 正在登录校內..."
	page = $agent.get("http://www.xiaonei.com")
	search_form = page.forms.first
	search_form.fields.name("email").value = "zjuliubin"
	search_form.fields.name("password").value = "mypass"
	$agent.submit(search_form)

	#	remove_car()
	page = $agent.get_file("http://carpark.xiaonei.com/getCarParkMainFrame.do")
	friends = parse_friends(page)
	get_money(page)
	get_myid(page)
	page = $agent.get_file("http://carpark.xiaonei.com/ajaxGetMyCar.f")
	cars = parse_mycar(page)

	puts "friends: #{friends.length}"
	puts "cars: #{cars.length}"
	cars.each do |car|
		#		if car["money"].to_i == 0 or car["money"].to_i > 10 then
		park_car(car, friends)
		#		end
	end
rescue Exception => e
	puts "#{e}\n"
end
